<?php

namespace App\Models\Cert;

use App\Models\Common\CommonModel;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class CertGo extends CommonModel
{
    use HasFactory;

    protected $table = 'cert_go';

    public function getDriver() {
        return $this->hasOne(CertDriver::class,'user_id','user_id');
    }

    public function getUser() {
        return $this->hasOne(User::class,'id','user_id');
    }

    public function getGoOther() {
        return $this->hasOne(CertGoOther::class,'user_id','user_id');
    }

    public function getRecords(array $conditions) {
        $records = $this->newQuery();
        if(!empty($conditions['phone'])){
            $records = $records->whereHas('getUser',function ($q) use ($conditions){
                $q->where('phone',$conditions['phone']);
            });
        }
        if(!empty($conditions['name'])){
            $records = $records->where('belong_to','like','%'.$conditions['name'].'%');
        }

        if(!empty($conditions['registerDay'])){
            $records = $records->whereDate('register_day','>=',$conditions['registerDay'][0])
                ->whereDate('register_day','<=', $conditions['registerDay'][1]);
        }
        if(!empty($conditions['givingDay'])){
            $records = $records->whereDate('giving_day','>=',$conditions['givingDay'][0])
                ->whereDate('giving_day','<=', $conditions['givingDay'][1]);
        }
        if(!empty($conditions['useWay']) && $conditions['useWay']!='all'){

            $records = $records->where('use_way',$conditions['useWay']);
        }


        return $records->orderBy('id','desc');
    }
}
